package com.demo.core.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.core.dao.FormDao;
import com.demo.core.dao.FormItemDao;
import com.demo.core.dao.FormItemExtendDao;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Form;
import com.demo.core.model.FormItem;
import com.demo.core.model.FormItemExtend;
import com.demo.core.model.support.converter.AdjacencyListToChainedObjectConverter;
import com.demo.core.service.base.FormService;
import com.demo.core.service.base.FormSourceService;

/**
 * 自定义表单服务
 * @author ryuu.kk
 *
 */
public class FormServiceImpl implements FormService {

	/**
	 * 自定义表单Dao
	 */
	private FormDao formDao;
	
	/**
	 * 自定义表单项Dao
	 */
	private FormItemDao formItemDao;
	
	/**
	 * 自定义表单项扩展属性Dao
	 */
	private FormItemExtendDao formItemExtendDao;
	
	/**
	 * FormSource服务
	 */
	private FormSourceService formSourceService;
	
	@Override
	public Long save(Form form) {
		Long formId = formDao.insert(form);
		List<FormItem> formItemList = form.getFormItemList();
		if (formItemList != null) {
			for (FormItem formItem : formItemList) {
				formItem.setFormId(formId);
				Long formItemId = formItemDao.insert(formItem);
				List<FormItemExtend> formItemExtendList = formItem.getFormItemExtendList();
				if (formItemExtendList != null) {
					for (FormItemExtend extend : formItemExtendList) {
						extend.setItemId(formItemId);
						formItemExtendDao.insert(extend);
					}
				}
			}
		}
		return formId;
	}

	@Override
	public Long save(FormItem item) {
		Long formItemId = formItemDao.insert(item);
		List<FormItemExtend> formItemExtendList = item.getFormItemExtendList();
		if (formItemExtendList != null) {
			for (FormItemExtend extend : formItemExtendList) {
				extend.setItemId(formItemId);
				formItemExtendDao.insert(extend);
			}
		}
		return formItemId;
	}

	@Override
	public void save(Long id, List<FormItem> itemList) {
		if (itemList != null) {
			for (FormItem formItem : itemList) {
				formItem.setFormId(id);
				formItemDao.insert(formItem);
			}
		}
	}

	@Override
	public Long save(FormItemExtend itemExtend) {
		return formItemExtendDao.insert(itemExtend);
	}
	
	@Override
	public boolean formExists(Long id) {
		return formDao.exists(id);
	}

	@Override
	public boolean formItemExists(Long id) {
		return formItemDao.exists(id);
	}

	@Override
	public boolean formItemExtendExists(Long id) {
		return formItemExtendDao.exists(id);
	}

	@Override
	public void updateFormStatus(Long id, Boolean enabled, String status) {
		Form form = new Form();
		form.setEnabled(enabled);
		form.setStatus(status);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		formDao.updateStatus(form, params);
	}

	@Override
	public void updateForm(Long id, String name, String title) {
		Form form = new Form();
		form.setName(name);
		form.setTitle(title);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		formDao.update(form, params);
	}

	@Override
	public void updateFormItemProperties(Long id, String name, String title, String action) {
		FormItem formItem = new FormItem();
		formItem.setName(name);
		formItem.setTitle(title);
		formItem.setAction(action);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		formItemDao.update(formItem, params);
	}

	@Override
	public void updateFormItemMapping(Long id, String key, String value, String type) {
		FormItem formItem = new FormItem();
		formItem.setKey(key);
		formItem.setValue(value);
		formItem.setType(type);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		formItemDao.update(formItem, params);
	}

	@Override
	public void updateFormItemHtml(Long id, String htmlId, String htmlElement, String htmlType, String innerHtml) {
		FormItem formItem = new FormItem();
		formItem.setHtmlId(htmlId);
		formItem.setHtmlElement(htmlElement);
		formItem.setHtmlType(htmlType);
		formItem.setInnerHtml(innerHtml);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		formItemDao.update(formItem, params);
	}

	@Override
	public void updateFormItemSource(Long id, String ds, String bindName, String bindValue) {
		FormItem formItem = new FormItem();
		formItem.setDs(ds);
		formItem.setBindName(bindName);
		formItem.setBindValue(bindValue);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		if (ds == null || ds.length() == 0) {
			this.clearFormItemSource(id);
		} else {
			formItemDao.update(formItem, params);
		}
	}

	@Override
	public void clearFormItemSource(Long itemId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", itemId);
		params.put("dsClear", itemId);
		formItemDao.update(new FormItem(), params);
	}
	
	@Override
	public void updateFormItemStatus(Long id, Boolean enabled, String status, int priority) {
		FormItem formItem = new FormItem();
		formItem.setEnabled(enabled);
		formItem.setStatus(status);
		formItem.setPriority(priority);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		formItemDao.updateStatus(formItem, params);
	}

	@Override
	public void updateFormItemExtendProperties(Long id, String name, String title) {
		FormItemExtend formItemExtend = new FormItemExtend();
		formItemExtend.setName(name);
		formItemExtend.setTitle(title);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		formItemExtendDao.update(formItemExtend, params);
	}

	@Override
	public void updateFormItemExtendMapping(Long id, String key, String value) {
		FormItemExtend formItemExtend = new FormItemExtend();
		formItemExtend.setKey(key);
		formItemExtend.setValue(value);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		//params.put("form", form);
		formItemExtendDao.update(formItemExtend, params);
	}

	@Override
	public void deleteForm(Long id) {
		formDao.delete(id);
	}

	@Override
	public void deleteFormItem(Long id) {
		formItemDao.delete(id);
	}

	@Override
	public void deleteFormItems(Long formId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("formId", formId);
		formItemDao.delete(params);		
	}
	
	@Override
	public void deleteFormItemExtend(Long id) {
		formItemExtendDao.delete(id);
	}
	
	@Override
	public void deleteFormItemExtends(Long formItemId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("itemId", formItemId);
		formItemExtendDao.delete(params);
	}

	@Override
	public Form findForm(Long formId) {
		return formDao.select(formId);
	}

	@Override
	public List<Form> findFormList(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		return formDao.select(params);
	}

	@Override
	public List<Form> findFormList(Long siteId, String i18n) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		params.put("i18n", i18n);
		return formDao.select(params);
	}

	@Override
	public Form queryForm(Long id) {
		Form form = formDao.select(id);
		return queryForm(form);
	}
	
	@Override
	public List<Form> queryFormList(Long siteId) {
		return queryFormList(siteId, null, null);
	}

	@Override
	public List<Form> queryFormList(Long siteId, String type) {
		return queryFormList(siteId, type, null);
	}

	@Override
	public List<Form> queryFormList(Long siteId, String type, Boolean enabled) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("type", type);
		params.put("enabled", enabled);
		
		List<Form> formList = formDao.select(params);
		if (formList != null) {
			for (Form form : formList) {
				queryForm(form);
			}
		}
		return formList;
	}

	@Override
	public Form queryTree(Long siteId) {
		return queryTree(siteId, null);
	}

	@Override
	public Form queryTree(Long siteId, Long pid) {
		Form result = null;
		Map<String, Object> params = new HashMap<String, Object>(5);
		params.put("siteId", siteId);
		params.put("pid", pid);
		List<Form> formList = formDao.tree(params);
		result = AdjacencyListToChainedObjectConverter.conver(formList);
		return result;
	}

	@Override
	public FormItem findFormItem(Long formItemId) {
		return formItemDao.select(formItemId);
	}
	
	@Override
	public List<FormItem> findFormItemList(Long formId, int groupType) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("formId", formId);
		params.put("groupType", groupType);
		return formItemDao.select(params);
	}

	@Override
	public List<FormItem> findFormItemList(Long formId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("formId", formId);
		return formItemDao.select(params);
	}

	@Override
	public FormItem queryFormItem(Long formItemId) {
		FormItem item = findFormItem(formItemId);
		return queryFormItem(item);
	}

	@Override
	public List<FormItem> queryFormItemList(Long formId, int groupType) {
		List<FormItem> formItemList = findFormItemList(formId, groupType);
		if (formItemList != null) {
			for (FormItem item : formItemList) {
				queryFormItem(item);
			}
		}
		return formItemList;
	}
	
	@Override
	public FormItemExtend findFormItemExtend(Long formItemExtendId) {
		return formItemExtendDao.select(formItemExtendId);
	}

	@Override
	public List<FormItemExtend> findFormItemExtendList(Long formItemId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("itemId", formItemId);
		return formItemExtendDao.select(params);
	}

	@Override
	public Map<String, List<FormItemExtend>> findFormItemExtendMap(Long formItemId, String key) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("itemId", formItemId);
		params.put("key", key);
		List<FormItemExtend> formItemExtendList = formItemExtendDao.select(params);
		Map<String, List<FormItemExtend>> formItemExtendMap = null;
		if (formItemExtendList != null) {
			formItemExtendMap = new HashMap<String, List<FormItemExtend>>();
			for (FormItemExtend extend : formItemExtendList) {
				String extendKey = extend.getKey();
				if (formItemExtendMap.containsKey(key)) {
					List<FormItemExtend> extendList = formItemExtendMap.get(extendKey);
					if (extendList == null) {
						extendList = new ArrayList<FormItemExtend>();
					}
					extendList.add(extend);
				} else {
					List<FormItemExtend> extendList = new ArrayList<FormItemExtend>();
					extendList.add(extend);
					formItemExtendMap.put(extend.getKey(), extendList);
				}
			}
		}
		return formItemExtendMap;
	}

	@Override
	public ResultSearchResult<Form> searchForm(Long siteId, String type, int firstResult, int maxResults) {
		return searchForm(siteId, type, firstResult, maxResults, "");
	}

	@Override
	public ResultSearchResult<Form> searchForm(Long siteId, String type, int firstResult, int maxResults, String... sort) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("type", type);
		SearchArgumentSupport args = new SearchArgumentSupport(firstResult, maxResults);
		args.setOrders(sort);
		return formDao.search(params, args);
	}
	
	private Form queryForm(Form form) {
		if (form != null) {
			List<FormItem> formItemList = findFormItemList(form.getId());
			List<FormItem> newFormItemList = new ArrayList<FormItem>(formItemList.size());
			if (formItemList != null) {
				for (FormItem item : formItemList) {
					item = queryFormItem(item.getId());
					newFormItemList.add(item);
				}
				form.setFormItemList(newFormItemList);
			}
			// 设置Form资源
			form.setFormSourceList(formSourceService.querySourceList(form.getId()));
		}
		return form;
	}
	
//	private FormItem queryFormItem(FormItem formItem) {
//		List<FormItemExtend> formItemExtendList = findFormItemExtendList(formItem.getId());
//		Map<String, List<FormItemExtend>> formItemExtendMap = new HashMap<String, List<FormItemExtend>>();
//		if (formItemExtendList != null) {
//			formItem.setFormItemExtendList(formItemExtendList);
//			for (FormItemExtend extend : formItemExtendList) {
//				String key = extend.getKey();
//				if (formItemExtendMap.containsKey(key)) {
//					List<FormItemExtend> extendList = formItemExtendMap.get(key);
//					if (extendList == null) {
//						extendList = new ArrayList<FormItemExtend>();
//					}
//					extendList.add(extend);
//				} else {
//					List<FormItemExtend> extendList = new ArrayList<FormItemExtend>();
//					extendList.add(extend);
//					formItemExtendMap.put(extend.getKey(), extendList);
//				}
//			}
//			formItem.setFormItenExtendMap(formItemExtendMap);
//		}
//		return formItem;
//	}

	private FormItem queryFormItem(FormItem formItem) {
		List<FormItemExtend> formItemExtendList = findFormItemExtendList(formItem.getId());
		Map<String, FormItemExtend> formItemExtendMap = new HashMap<String, FormItemExtend>();
		if (formItemExtendList != null) {
			formItem.setFormItemExtendList(formItemExtendList);
			for (FormItemExtend extend : formItemExtendList) {
				String key = extend.getKey();
				if (formItemExtendMap.containsKey(key)) {
					// 重复数据错误
				} else {
					formItemExtendMap.put(extend.getKey(), extend);
				}
			}
			formItem.setFormItenExtendMap(formItemExtendMap);
		}
		return formItem;
	}
	
	public void setFormDao(FormDao formDao) {
		this.formDao = formDao;
	}

	public void setFormItemDao(FormItemDao formItemDao) {
		this.formItemDao = formItemDao;
	}

	public void setFormItemExtendDao(FormItemExtendDao formItemExtendDao) {
		this.formItemExtendDao = formItemExtendDao;
	}

	public void setFormSourceService(FormSourceService formSourceService) {
		this.formSourceService = formSourceService;
	}
}
